﻿#pragma once

#include "Framework/Application.h"
#include "Output/Console.h"

#include "Misc/Guid.h"

#include "Path/Paths.h"

#include "Asset/AssetSubsystem.h"
#include "Mesh/StaticMesh.h"

#include "Resource/StaticMeshUtils.h"

#include "AssetTestObject.h"
#include "AssetTestObject2.h"

#include "Texture/Texture2D.h"


// void TestStaticMesh(RApplicationPtr App)
// {

//     //create a asset 
//     auto Asset=NewObject<RObject>(nullptr);
//     Asset->ID=SGuid::NewGuid();

//     WH::Cout()<<"AssetID :"<<Asset->ID.ToString() <<WH::EndL();

//     auto ChairMesh=RStaticMeshUtils::LoadFromGLTF(SPaths::GetContentDirectory()/U"EngineEditor" / U"SM_Chair.glb");
//     ChairMesh->ID=SGuid::NewGuid();
//     ChairMesh->SetOwner(Asset.Get());
    


//     auto  Info=App->GetSubsystem<RAssetSubsystem>()->SaveTo(Asset,U"D:/1.Asset");
//     Asset->Destroy();

//     auto LoadedAsset=App->GetSubsystem<RAssetSubsystem>()->LoadFrom(Info,nullptr);
//     LoadedAsset[0]->SetAssetID(SGuid::NewGuid());

//     auto  Info2=App->GetSubsystem<RAssetSubsystem>()->SaveTo(LoadedAsset[0],U"D:/2.Asset");

//     WH::Cout()<<"Done"<<WH::EndL();


// }

void LoadAsset(const SAssetPath& InAssetPath)
{   
    RAssetSubsystem::Get()->AsyncLoadAsset(InAssetPath
        ,EAssetSection ::MainObjEditObjAndThumbnail
        ,TDelegate<void, SAssetLoadResult >::Create([](const SAssetLoadResult& InLoadResult){
            WH::Cout()<<"Load Asset "<<InLoadResult.MainObject->GetPath()<<WH::EndL();
            WH::Cout()<<"Load EditorAsset "<<InLoadResult.EditInfoObject->GetPath()<<WH::EndL();

            RTexture2DPtr Texture= InLoadResult.MainObject;
            Texture->SaveToPNG(Texture,SPaths::GetContentDirectory()/U"EngineEditor"/U"Test" / U"TestPNG.png");
            
            RApplication::GetApplication()->Exit();

        })
        );

}



void TestTexture(RApplicationPtr App)
{

    //create a asset 
    // auto Asset=NewObject<RObject>(nullptr);
    // Asset->ID=SGuid::NewGuid();
    // Asset->Rename(U"TestAssetMainObject");

    auto Texture=RTexture2D::CreateFromImage(U"D:/ShareEngine/WuhuaEngine/Engine/Modules/EngineCore/Source/Icons/AddFile.png");
    //auto ChairMesh=RStaticMeshUtils::LoadFromGLTF(SPaths::GetContentDirectory()/U"EngineEditor" / U"SM_Chair.glb");
    Texture->SetNodeName (U"TestAssetMainObject");

    auto EditorAsset=NewObject<RObject>();
    EditorAsset->SetNodeName(U"TestAssetEditorObject");


    SGuid AssetID = SGuid::NewGuid();
    WH::Cout()<<"AssetID :"<<AssetID.ToString() <<WH::EndL();

    RAssetSubsystem::Get()->AsyncSaveAsset(
        SAssetPath(U"RTexture2D EngineEditor /Test/Test2 TestTexture " + AssetID.ToString() )
        ,Texture
        ,EditorAsset
        ,nullptr
        ,TDelegate<void,SAssetHead>::Create([AssetID](SAssetHead){
            WH::Cout()<<"Save Completed"<<WH::EndL();

            LoadAsset(SAssetPath(U"RTexture2D EngineEditor /Test/Test2 TestTexture " + AssetID.ToString() ));


            //RApplication::GetApplication()->Exit();
        })
        );

    // auto  Info=App->GetSubsystem<RAssetSubsystem>()->SaveTo(Asset,U"D:/1.Asset");
    // Asset->Destroy();

    // auto LoadedAsset=App->GetSubsystem<RAssetSubsystem>()->LoadFrom(Info,nullptr);
    // LoadedAsset[0]->SetAssetID(SGuid::NewGuid());

    // auto  Info2=App->GetSubsystem<RAssetSubsystem>()->SaveTo(LoadedAsset[0],U"D:/2.Asset");

    WH::Cout()<<"Done"<<WH::EndL();



}

void TestSaveObject()
{
    auto Main=NewObject<RAssetTestObject>();
    Main->tP=44;
    Main->PPP=356;
    Main->TestSubObject->P=55;
    Main->TestSubObject->TestArray2.Resize(5);
    Main->TestSubObject->TestArray2[0]=U'a';
    Main->TestSubObject->TestArray2[1]=U'b';
    Main->TestSubObject->TestArray2[2]=U'c';
    Main->TestSubObject->TestArray2[3]=U'd';
    Main->TestSubObject->TestArray2[4]=U'\0';

    Main->TestArray.Resize(5);
    Main->TestArray[0]=U'A';
    Main->TestArray[1]=U'B';
    Main->TestArray[2]=U'C';
    Main->TestArray[3]=U'D';
    Main->TestArray[4]=U'\0';

    SGuid AssetID = SGuid(1234,1234,1234,1234);

    auto EditorAsset=NewObject<RObject>();
    EditorAsset->SetNodeName(U"TestAssetEditorObject");

    RAssetSubsystem::Get()->AsyncSaveAsset(
    SAssetPath(U"RAssetTestObject EngineEditor ./Test/Test2 TestAsset " + AssetID.ToString() )
    ,Main
    ,EditorAsset
    ,nullptr
    ,TDelegate<void,SAssetHead>::Create([AssetID](SAssetHead){
        WH::Cout()<<"Save Completed"<<WH::EndL();
        RApplication::GetApplication()->Exit();
    })
    );

}

void TestLoadAsset()
{
    SGuid AssetID = SGuid(1234,1234,1234,1234);
    SAssetPath AssetPath(U"RAssetTestObject EngineEditor ./Test/Test2 TestAsset " + AssetID.ToString());

    RAssetSubsystem::Get()->AsyncLoadAsset(AssetPath
        ,EAssetSection ::MainObjEditObjAndThumbnail
        ,TDelegate<void,SAssetLoadResult>::Create([](const SAssetLoadResult& InLoadResult){
            WH::Cout()<<"Load Asset "<<InLoadResult.MainObject->GetPath()<<WH::EndL();
            WH::Cout()<<"Load EditorAsset "<<InLoadResult.EditInfoObject->GetPath()<<WH::EndL();

            auto Main= RAssetTestObjectPtr(InLoadResult.MainObject);

            WH::Cout()<<U"tP:"<< Main->tP <<WH::EndL();
            WH::Cout()<<U"PPP:"<< Main->PPP <<WH::EndL();
            WH::Cout()<<U"Subobject p:"<< Main->TestSubObject->P <<WH::EndL();
            WH::Cout()<<U"ArrayNum2:"<<Main->TestSubObject->TestArray2.Num()  <<WH::EndL();
            WH::Print(Main->TestSubObject->TestArray2[0]);
            WH::Print(Main->TestSubObject->TestArray2[1]);
            WH::Cout()<<U"ArrayNum:"<<Main->TestArray.Num()  <<WH::EndL();
            WH::Print(Main->TestArray[0]);
            WH::Print(Main->TestArray[1]);
            
            
            RApplication::GetApplication()->Exit();

        })
        );


}

int main(int argc, char* argv[])
{
    auto App=NewObject<RApplication>();

    SApplicationArgument Argument;
    Argument.ExecutablePath=SPath(argv[0]);

    App->Initialize(Argument);

    SAssetPackage Package;
    Package.Name=U"EngineEditor";
    Package.Path=
        SPaths::GetExecutablePath().Parent().ToString();
    RAssetSubsystem::Get()->AddPackage(Package);

    //TestTexture(App);
    //TestSaveObject();
    TestLoadAsset();

    App->Run();
}